<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
@session_start();
include('functions.php.inc');
if (isset($_POST['beer_name'])) {
	if (trim($_POST['beer_name']) != "") {
		setName($_SESSION['probe'],$_POST['beer_name']);
	}
	if (isset($_POST['beer_temp_variance'])) {
		if (trim($_POST['beer_temp_variance']) != "") {
			setVariance($_SESSION['probe'],$_POST['beer_temp_variance']);
		}
	}
	//we got a post update the data.
	if (isset($_POST['beer_target_temp'])) {
		if (is_numeric($_POST['beer_target_temp'])) {
			setTargetTemp($_SESSION['probe'],$_POST['beer_target_temp']);
		}
	}
	if (isset($_POST['beer_hlt'])) {
		if (trim($_POST['beer_hlt']) != "") {
			setHltProbe($_SESSION['probe']);
		} else {
			setHltProbe(0);	
		}
	} else {
		setHltProbe(0);			
	}

	//this doesn't work right, this resets the glycol
	if (isGlycol($_SESSION['probe'])) {
		if (isset($_POST['beer_glycol'])) {
			if (trim($_POST['beer_glycol']) != "") {
				setGlycolProbe($_SESSION['probe']);
			} else {
				setGlycolProbe(0);	
			}
		} else {
			setGlycolProbe(0);			
		}
	}

	if (isset($_POST['beer_mash'])) {
		if (trim($_POST['beer_mash']) != "") {
			setMashProbe($_SESSION['probe']);
		} else {
			setMashProbe(0);
		}
	} else {
		setMashProbe(0);
	}
	if (isset($_POST['beer_og'])) {
		if (trim($_POST['beer_og']) != "") {
			setOG($_SESSION['probe'],$_POST['beer_og']);
		}
	}
	if (isset($_POST['beer_off'])) {
		if (trim($_POST['beer_off']) != "") {
			setOff($_SESSION['probe']);
		}
	} else {
		setOn($_SESSION['probe']);
	}
}

$probe = 1;
if (isset($_GET['probe'])) {
	if (trim($_GET['probe']) != "") {
		if (is_numeric($_GET['probe'])) {
			$probe = $_GET['probe'];
		}
	}
}
include('sql.php.inc');

//get the real+probe
$probe_save = $probe;
$sql = "SELECT real_probe from probemap where probe=\"$probe\"";
$result = mysql_query($sql,$connect);
$row = mysql_fetch_row($result);
$probe = $row[0];
//if ($probe == "") {
//	$probe = $probe_save;
//}

$_SESSION['probe']=$probe;


$isMash = isMASH($probe);
$isHLT = isHLT($probe);
$isGlycol = isGlycol($probe);
?>
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>   

<script>
function createLabel() {
	var name = document.getElementById('beer_name').value;
	var og = document.getElementById('label_og').value;
	var fg = document.getElementById('label_fg').value;
	var copies = document.getElementById('label_copies').value;
	var d = document.getElementById('label_date').value;
	var query="";
	if (name != "") {
		if (query != "") {
			query+="&";
		}
		query += "name="+name;
	}
	if (og != "") {
		if (query != "") {
			query+="&";
		}
		query += "og="+og;
	}
	if (fg != "") {
		if (query != "") {
			query+="&";
		}
		query += "fg="+fg;
	}
	if (d != "") {
		if (query != "") {
			query+="&";
		}
		query += "date="+d;
	}
	if (copies != "") {
		if (query != "") {
			query+="&";
		}
		query += "copies="+copies;
	} else {
		if (query != "") {
			query+="&";
		}
		query += "copies=1";
	}
	
	$.get('make.php?'+query, function(data) {
		if (copies == 1) {
			alert("Your label was printed");
		} else {
			alert("Your labels were printed");
		}
	});
}
function showAbout() {
	alert("Beerwatch 0.0.1\n\nBy John Hass john@sipmeeting.com\nDesigned on the Raspberry Pi\n\nKeep your beer in check!\n\nGPL V2 see LICENSE file for more details\n");
}
function disp_confirm() {
	var r=confirm("Are you sure you wish to remove all information for probe <?php echo $_SESSION['probe']?>")
	if (r==true) {
		window.location = "clearprobe.php?probe=<?php echo $_SESSION['probe'] ?>";
	}
}

function getTemp(probe) {
	$.get('gettemp.php?probe='+probe, function(data) {
		if (data != "") {
			document.getElementById('current_temp').innerHTML = data;
		}
	});	
}

$(document).ready(function() {
	getTemp('<?php echo $probe ?>');
	setTimeout(getTemp('<?php echo $probe ?>'), 30000);

});

</script>
<title>Welcome to Beerwatch!</title>
</head>
<body>
<center><h1>Beer watch <a href="#" onClick="showAbout()"><img height="64" width="64" src="img/beer_logo.png" /></a></h1></center><br />
Detected Probes: 
<?php
for ($i = 0;$i<getLinks();$i++) {
	$c = $i;
	$c++;
	echo "<a href=\"index.php?probe=$c\">$c</a>&nbsp;&nbsp;\n";
}
?>
<hr />
<?php 
if (isGlycol($probe)) {
?>
	Glycol Freezer	
<?php
} else {
?>
Recipe <?php echo $probe_save ?>: <?php echo getName($probe) ?><br />
<?php
}
?>
<table border="0">
<form method="post" action="index.php?<?php echo "probe=".$probe_save ?>">
<tr>
	<td>Hot Liquor Tank</td>
	<td><input type="checkbox" name="beer_hlt" <?php if(isHLT($probe)) { echo "checked"; } ?>></td>
</tr>
<tr>
	<td>Is Mash</td>
	<td><input type="checkbox" name="beer_mash" <?php if(isMASH($probe)) { echo "checked"; } ?>></td>
</tr>
<tr>
	<td>Is Glycol</td>
	<td><input type="checkbox" name="beer_glycol" <?php if(isGlycol($probe)) { echo "checked"; } ?>></td>
</tr>
<tr>
	<td>Off</td>
	<td><input type="checkbox" name="beer_off" <?php if(isOff($probe)) { echo "checked"; } ?>></td>
</tr>
<?php
if ($isMash || $isHLT || $isGlycol) {
?>
<?php
} else {
?>
<tr>
	<td>Name</td>
	<td><input type="text" id="beer_name" name="beer_name" value="<?php echo getName($probe) ?>" ></td>
</tr>
<tr>
	<td>Target Temp</td>
	<td><input type="text" name="beer_target_temp" value="<?php echo targetTemp($probe) ?>"></td>
</tr>
<tr>
	<td>Temp Variance</td>
	<td><input type="text" name="beer_temp_variance" value="<?php echo variance($probe) ?>"></td>
</tr>
<?php
}
if ($isHLT) {
?>
	<input type="hidden" name="beer_name" value="Hot Liquor Tank">
	<input type="hidden" name="beer_temp_variance" value="0">
	<tr>
       		<td>Target Temp</td>
       		<td><input type="text" name="beer_target_temp" value="<?php echo targetTemp($probe) ?>"> Calculated temp: <?php echo getRealTemp($probe); ?></td>
	</tr>
<?php
}
if ($isMash) {
?>
	<input type="hidden" name="beer_name" value="Mash">

<?php
}
?>
<?php
if (!$isMash && !$isHLT && !$isGlycol) {
?>
<tr>
	<td>O.G.</td>
	<td><input type="text" name="beer_og" value="<?php echo getOG($probe); ?>"></td>
</tr>
<?php
}
?>
<tr>
	<td></td>
	<td><input type="submit" value="Update"></td>
</tr>
</form>
<tr>
	<td>Current Temp</td>
	<td><span id="current_temp">Getting the current temp</span></td>
</tr>
<?php
if (!$isMash && !$isGlycol) {
?>
<tr>
	<td>Heat On</td>
	<td><?php echo heatOn($probe_save); ?></td>
</tr>
<?php
}
?>
<?php
	if ($isGlycol) {
?>
	<td>Pump On</td>
	<td><?php echo glycolOn($probe); ?></td>
<?php
	}
?>
<tr>
	<td valign="top">Last 24 Hours</td>
	<td align="top"><img src="munin/beerwatch/beerwatch/beerwatch_<?php echo $probe ?>-day.png" />
</tr>
</table>
<br />
<br />
<?php
if (!$isMash && !$isHLT && !$isGlycol) {
?>
<hr />
<table border=0>
<tr>
	<td colspan="2" align="center">Make A Label</td>
</tr>
<tr>
	<td>Date:</td>
	<td><input type="text" name="label_date" id="label_date" value="<?php echo date("Y-m-d"); ?>"></td>
</tr>
<tr>
	<td>O.G.:</td>
	<td><input type="text" name="og" id="label_og" value="<?php echo getOG($probe); ?>"></td>
</tr>
<tr>
	<td>F.G.:</td>
	<td><input type="text" name="fg" id="label_fg" value=""></td>
</tr>
<tr>
	<td>Copies:</td>
	<td><input type="text" id="label_copies" name="label_copies" value="1" size=1 maxlength=1></td>
</tr>
<tr>
<td align="center" colspan=2><input type="button" onClick="createLabel()" name="make_labels" value="Make Label"</td>
</tr>
</table>
<?php
}
?>
<br />
<br />
<hr />
<input type="button" value="Clear Probe <?php echo $probe?>" onClick="disp_confirm()">
</body>
</html>
